#include <iostream>
#include <memory>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std::placeholders;
using namespace std;

class A
{
public:
    void print(int n1, int n2, int n3)
    {
        cout << n1 << " " << n2 << " " << n3 << endl;
    }
};

void fun1(int n1, int n2)
{
    cout << n1 << " " << n2  << endl;
}

struct cmp
{
    bool operator()(int x, int y)
    {
        return x < y;
    }    
};


int main()
{
    //原fun1接受三个参数，其中绑定了2个，第三个参数由新的可调用对象指定  
    auto f1 = bind(fun1, 11, _1);
    f1(33);

    A a;
    //类成员函数需要绑定该类的this指针  
    auto f2 = bind(&A::print, &a, _2, 22, _1);
    f2(44,55);

    vector<int> table = {1, 2, 3, 1, 3, 2, 1, 3, 5};
    auto f3 = bind(cmp(), _1, 4);
    auto f4 = bind(cmp(), 2, _1);
    cout << "nums less than 4 in table is " << count_if(table.begin(), table.end(), f3) << endl;
    cout << "nums bigger than 2 in table is " << count_if(table.begin(), table.end(), f4) << endl;

    cout << "test of not1 " << endl;
    auto f5 = not1(bind2nd(less<int>(), 2));
    cout << "nums bigger than 2 in table is " << count_if(table.begin(), table.end(), f5) << endl;

    auto f6 = bind(less<int>(), _1, 2);
    cout << "nums bigger than 2 in table is " << count_if(table.begin(), table.end(), f5) << endl;
}